package zservers

//
//import (
//	"github.com/uber/tchannel-go/thrift"
//	"go.didapinche.com/foundation/plat-echo/internal/app/ops/models"
//	"go.didapinche.com/foundation/plat-echo/internal/app/ops/services"
//	"go.didapinche.com/foundation/plat-echo/internal/pkg/es"
//	"go.didapinche.com/foundation/plat-echo/internal/pkg/utils"
//	"go.didapinche.com/goapi/echo_api"
//	"go.uber.org/zap"
//	"strconv"
//)
//
//type echoSlaService struct {
//	logger            *zap.Logger
//	graphPanelService services.GraphPanelService
//}
//
//func NewEchoSlaService(logger *zap.Logger, graphPanelService services.GraphPanelService) echo_api.TChanEchoSlaService {
//	return &echoSlaService{
//		graphPanelService: graphPanelService,
//		logger:            logger,
//	}
//}
//
//func (s *echoSlaService) UpdateAlertRule(ctx thrift.Context, alertRule *echo_api.AlertRule) error {
//	name := alertRule.Type + "-" + strconv.FormatInt(alertRule.ID, 10)
//	return s.graphPanelService.UpdateAlertResource(name, alertRule)
//}
//
//func (s *echoSlaService) DeleteAlertRule(ctx thrift.Context, alertRule *echo_api.AlertRule) error {
//	name := alertRule.Type + "-" + strconv.FormatInt(alertRule.ID, 10)
//	return s.graphPanelService.DeleteAlertResource(name)
//}
//
//func (s *echoSlaService) FindTraceInfo(ctx thrift.Context, serviceName string, appName string, startTime int64, endTime int64) (*echo_api.ServiceTraceInfo, error) {
//	start := utils.ConvertTime(startTime)
//	end := utils.ConvertTime(endTime)
//	typeEnum := models.API_ZEUS_SERVER
//	if len(serviceName) == 0 {
//		typeEnum = models.API_HTTP
//	}
//
//	serviceData, err := s.FindServiceData(ctx, serviceName, appName, startTime, endTime)
//	if serviceData == nil {
//		s.logger.Error("call FindServiceData error", zap.Error(err))
//		return nil, nil
//	}
//
//	traceInfo := new(echo_api.ServiceTraceInfo)
//	traceInfo.ServiceInfo = serviceData
//
//	parentsMap, childrenMap := s.jaegerClient.GetParentsSpan(appName, 60*24*3)
//	s.logger.Info(fmt.Sprintf("parents = %v, children = %v", parentsMap, childrenMap))
//
//	var parents []string
//	var children []string
//	for name, _ := range parentsMap {
//		parents = append(parents, name)
//	}
//	for name, _ := range childrenMap {
//		children = append(children, name)
//	}
//	parentsChan := make(chan []*echo_api.ServiceData)
//	if len(parents) > 0 {
//		go func() {
//			now := time.Now()
//			var parentsData []*echo_api.ServiceData
//
//			datas, err := s.dataService.FindByAppNames(parents, typeEnum, start, end)
//			if err != nil {
//				s.logger.Error("parents call FindServiceData error", zap.Error(err))
//				parentsChan <- parentsData
//				return
//			}
//			for _, data := range datas {
//				parentsData = append(parentsData, convertServiceData(data))
//			}
//			s.logger.Info(fmt.Sprintf("parents call dataService.FindByAppNames cost = %d", (time.Now().UnixNano()-now.UnixNano())/1e6))
//			parentsChan <- parentsData
//		}()
//	}
//
//	childrenChan := make(chan []*echo_api.ServiceData)
//
//	if len(children) > 0 {
//		go func() {
//			now := time.Now()
//			var childrenData []*echo_api.ServiceData
//			datas, err := s.dataService.FindByAppNames(children, typeEnum, start, end)
//			if err != nil {
//				s.logger.Error("children call FindByAppNames error", zap.Error(err))
//				childrenChan <- childrenData
//				return
//			}
//
//			for _, data := range datas {
//				childrenData = append(childrenData, convertServiceData(data))
//			}
//			s.logger.Info(fmt.Sprintf("children call dataService.FindByAppNames cost = %d", (time.Now().UnixNano()-now.UnixNano())/1e6))
//			childrenChan <- childrenData
//		}()
//	}
//
//	if len(parents) > 0 {
//		traceInfo.Parents = <-parentsChan
//	}
//
//	if len(children) > 0 {
//		traceInfo.Children = <-childrenChan
//	}
//
//	return traceInfo, nil
//}
//
//func (s *echoSlaService) FindServiceData(ctx thrift.Context, serviceName string, appName string, startTime int64, endTime int64) (*echo_api.ServiceData, error) {
//	start := utils.ConvertTime(startTime)
//	end := utils.ConvertTime(endTime)
//
//	typeEnum := models.API_ZEUS_SERVER
//	if len(serviceName) == 0 {
//		typeEnum = models.API_HTTP
//	}
//	serviceData, err := s.dataService.GetByService(serviceName, appName, typeEnum, start, end)
//	if err != nil {
//		s.logger.Error("call GetByService error", zap.Error(err))
//		return nil, nil
//	}
//
//	return convertServiceData(serviceData), nil
//}
//
//func convertServiceData(serviceData *models.ServiceData) *echo_api.ServiceData {
//	data := new(echo_api.ServiceData)
//	data.SLA = serviceData.SLA
//	data.AppName = serviceData.AppName
//	data.ServiceName = serviceData.ZeusService
//	data.QPS, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", serviceData.QPS), 64)
//	data.ErrorCount, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", serviceData.ErrorCount), 64)
//	data.QpsMax, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", serviceData.QPSMax), 64)
//	data.TotalRequestCount, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", serviceData.TotalRequestCount), 64)
//	data.Tp95, _ = strconv.ParseFloat(fmt.Sprintf("%.3f", serviceData.TP95*1000), 64)
//	data.Tp99, _ = strconv.ParseFloat(fmt.Sprintf("%.3f", serviceData.TP99*1000), 64)
//	data.Tp95Max, _ = strconv.ParseFloat(fmt.Sprintf("%.3f", serviceData.TP95Max*1000), 64)
//	data.Tp99Max, _ = strconv.ParseFloat(fmt.Sprintf("%.3f", serviceData.TP99Max*1000), 64)
//	if data.ErrorCount == 0 || data.TotalRequestCount == 0 {
//		data.ErrorRate = 0
//	} else {
//		data.ErrorRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", data.ErrorCount*100/data.TotalRequestCount), 64)
//	}
//	return data
//}
